package com.hbx.residential_service.dao;

import com.hbx.residential_service.domain.Admin;
import com.hbx.residential_service.domain.Complaint;
import com.hbx.residential_service.domain.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface ComplaintDao {
    @Select("select * from complaints")
    @Results({
            @Result(id=true,property = "cpId",column = "cpId"),
            @Result(property = "cpTitle",column = "cpTitle"),
            @Result(property = "cpContent",column = "cpContent"),
            @Result(property = "cpDate",column = "cpDate"),
            @Result(property = "cpStatus",column = "cpStatus"),
            @Result(property = "cpHandleDate",column = "cpHandleDate"),

            @Result(property = "user",column = "cpUserId",javaType = User.class,one = @One(select = "com.hbx.residential_service.dao.UserDao.findById")),
            @Result(property = "admin",column = "cpAdminId",javaType = Admin.class,one = @One(select = "com.hbx.residential_service.dao.AdminDao.findById"))
    })
    List<Complaint> findAll(int page, int size);

    @Select("select * from complaints where cpId=#{cpId}")
    @Results({
            @Result(id=true,property = "cpId",column = "cpId"),
            @Result(property = "cpTitle",column = "cpTitle"),
            @Result(property = "cpContent",column = "cpContent"),
            @Result(property = "cpDate",column = "cpDate"),
            @Result(property = "cpStatus",column = "cpStatus"),
            @Result(property = "cpHandleDate",column = "cpHandleDate"),

            @Result(property = "user",column = "cpUserId",javaType = User.class,one = @One(select = "com.hbx.residential_service.dao.UserDao.findById")),
            @Result(property = "admin",column = "cpAdminId",javaType = Admin.class,one = @One(select = "com.hbx.residential_service.dao.AdminDao.findById"))
    })
    Complaint findDetailById(int cpId);

    @Delete("delete from complaints where cpId=#{cpId}")
    void deleteComplaint(int cpId);

    @Update("update complaints set cpStatus=#{cpStatus},cpHandleDate=#{cpHandleDate},cpUserId=#{user.userid},cpAdminId=#{admin.adminid} where cpId=#{cpId}")
    void UpdateCPById(Complaint complaint);

    @Select("select * from complaints where cpUserId in (select userid from owners where username like #{content})")
    @Results({
            @Result(id=true,property = "cpId",column = "cpId"),
            @Result(property = "cpTitle",column = "cpTitle"),
            @Result(property = "cpContent",column = "cpContent"),
            @Result(property = "cpDate",column = "cpDate"),
            @Result(property = "cpStatus",column = "cpStatus"),
            @Result(property = "cpHandleDate",column = "cpHandleDate"),

            @Result(property = "user",column = "cpUserId",javaType = User.class,one = @One(select = "com.hbx.residential_service.dao.UserDao.findById")),
            @Result(property = "admin",column = "cpAdminId",javaType = Admin.class,one = @One(select = "com.hbx.residential_service.dao.AdminDao.findById"))
    })
    List<Complaint> fsByUsername(@Param("content") String content,@Param("page") int page, @Param("size") int size);

    @Select("select * from complaints where cpStatus=0")
    @Results({
            @Result(id=true,property = "cpId",column = "cpId"),
            @Result(property = "cpTitle",column = "cpTitle"),
            @Result(property = "cpContent",column = "cpContent"),
            @Result(property = "cpDate",column = "cpDate"),
            @Result(property = "cpStatus",column = "cpStatus"),
            @Result(property = "cpHandleDate",column = "cpHandleDate"),

            @Result(property = "user",column = "cpUserId",javaType = User.class,one = @One(select = "com.hbx.residential_service.dao.UserDao.findById")),
            @Result(property = "admin",column = "cpAdminId",javaType = Admin.class,one = @One(select = "com.hbx.residential_service.dao.AdminDao.findById"))
    })
    List<Complaint> findByUnHandle(int page, int size);

}
